package com.chkj.fbook.system.service.impl;

import com.chkj.fbook.common.utils.IDUtils;
import com.chkj.fbook.system.service.AppService;
import com.chkj.fbook.system.service.MenuService;
import com.chkj.fbook.system.dao.AppDao;
import com.chkj.fbook.system.domain.AppDO;
import com.chkj.fbook.system.domain.MenuDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class AppServiceImpl implements AppService {

    @Resource
    private AppDao appDao;
    @Autowired
    private MenuService menuService;

    @Override
    public AppDO get(String uuid) {
        return appDao.get(uuid);
    }

    @Override
    public AppDO getByCode(String code) {
        return appDao.getByCode(code);
    }

    @Override
    public List<AppDO> list(Map<String, Object> map) {
        return appDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return appDao.count(map);
    }

    @Override
    public int save(AppDO app) {
        app.setUuid(IDUtils.genUUID());
        app.setCreatetime(new Date());
        int i1 = appDao.save(app);

        //添加应用对应的系统权限信息
        MenuDO menu = new MenuDO();
        menu.setAppId(app.getUuid());
        menu.setName(app.getName());
        menu.setPerms(app.getCode());
        menu.setParentId("0");//根目录为所有系统顶级目录
        menu.setType(0);//类型 0：目录 1：菜单 2：按钮
        menu.setIcon("fa fa-bank");
        menu.setOrderNum(0);
        int i2 = menuService.save(menu);

        return i1 + i2;
    }

    @Override
    public int update(AppDO app) {
        return appDao.update(app);
    }

    @Override
    public int remove(String uuid) {
        return appDao.remove(uuid);
    }

    @Override
    public int batchRemove(String[] uuids) {
        return appDao.batchRemove(uuids);
    }

    @Override
    public boolean isExists(AppDO app) {
        return (Integer) appDao.isExists(app) > 0;
    }

}
